package top.lixunda.ecommerce.server.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.user.api.controller.IUserMenuController;
import top.lixunda.ecommerce.server.user.api.entity.db.UserMenuDO;
import top.lixunda.ecommerce.server.user.api.entity.vo.SystemMenuTreeVO;
import top.lixunda.ecommerce.server.user.api.entity.vo.SystemMenuVO;
import top.lixunda.ecommerce.server.user.service.IUserMenuService;

import java.util.List;

/**
 * 用户菜单权限查询接口
 *
 * @author lixunda
 * @version time:2020/4/26 15:46
 */
@Slf4j
@RestController
public class UserMenuController extends BaseController implements IUserMenuController {

    private final IUserMenuService userMenuService;

    @Autowired
    public UserMenuController(IUserMenuService userMenuService) {
        this.userMenuService = userMenuService;
    }

    /**
     * 查询当前登陆用户拥有权限的所有菜单目录信息所有菜单目录信息
     *
     * @return 所有菜单目录信息
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryAll() {
        List<UserMenuDO> list = userMenuService.queryAll();
        List<SystemMenuVO> result = SystemMenuVO.fromUserMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 查询当前登陆用户拥有权限的所有菜单目录信息所有菜单目录信息树形结构
     *
     * @return 所有菜单目录信息树形结构
     */
    @Override
    public MessageObject<List<SystemMenuTreeVO>> queryAllTree() {
        List<UserMenuDO> list = userMenuService.queryAll();
        List<SystemMenuTreeVO> result = SystemMenuTreeVO.fromUserMenuToTree(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 根据菜单编码查询当前登陆用户拥有权限的某一个子系统的菜单目录
     *
     * @param menuCode 菜单编码，唯一索引
     * @return 该子系统的菜单目录信息
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryOneRootAllByCode(String menuCode) {
        List<UserMenuDO> list = userMenuService.queryOneRootAllByCode(menuCode);
        List<SystemMenuVO> result = SystemMenuVO.fromUserMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 根据菜单编码查询当前登陆用户拥有权限的某一个子系统的菜单目录树形结构数据
     *
     * @param menuCode 菜单编码，唯一索引
     * @return 该子系统的菜单目录信息树形结构数据
     */
    @Override
    public MessageObject<SystemMenuTreeVO> queryOneRootAllTreeByCode(String menuCode) {
        List<UserMenuDO> list = userMenuService.queryOneRootAllByCode(menuCode);
        List<SystemMenuTreeVO> result = SystemMenuTreeVO.fromUserMenuToTree(list);
        if (ObjectValidators.isEmpty(result)) {
            return getErrorMessageObject("查询菜单失败！结果为空！", null);
        } else {
            return getSuccessMessageObject("查询菜单成功！", result.get(0));
        }
    }

    /**
     * 根据父节点id获取当前登陆用户拥有权限的子类节点列表
     *
     * @param id 父节点id
     * @return 子节点列表
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryChildrenByParentId(Integer id) {
        List<UserMenuDO> list = userMenuService.queryChildrenByParentId(id);
        List<SystemMenuVO> result = SystemMenuVO.fromUserMenuDO(list);
        return getSuccessMessageObject("查询菜单成功！", result);
    }

    /**
     * 查询首层节点
     *
     * @return 首层节点
     */
    @Override
    public MessageObject<List<SystemMenuVO>> queryRoots() {
        List<UserMenuDO> list = userMenuService.queryRoots();
        List<SystemMenuVO> result = SystemMenuVO.fromUserMenuDO(list);
        return getSuccessMessageObject("查询首层菜单成功！", result);
    }
}
